﻿@inherits Vowei.Web.G18nWebViewPage<Vowei.Core.Models.Requirement>
@using System.Web.Mvc.Html 
@using Vowei.Data.Models
@using Vowei.Web.Models
@using Vowei.Web.Helpers
@using Vowei.Core.Models;

@{
    ViewBag.Title = "编辑需求详细信息";    
}
<form method="post" action="/Requirement/Edit">
    <div id="viewpot">
        <!-- place holder for template generated html -->
        <div class="references_container" id="children_container">
            <h2>@T("子任务")</h2>
            <ul class="references" id="children">
            @foreach (var item in Model.Children)
            {            
                <li class="reference">
                    <strong>@T("名称：")</strong>(<a href="/Ticket/Details/@item.ID">@item.ID</a>): @item.Title
                    <a href="javascript:" class="delete_btn">@T("删除")</a>
                    <input type="hidden" value="@item.ID" id="Children" name="Children" />
                </li>  
            }
            </ul>
            <input type="button" id="addChildren" alt="/Content/tmpl/RequirementIndex.html" value="@T("添加子工作任务")" />
        </div>
    </div>
   
    <p>
        <input type="button" name="submit_btn" id="submit_btn" value="@T("保存")" />
    </p>
</form>
<div id="action_dialog" style="display:none">
    @if (ViewData.ContainsKey("ProjectId"))
    {
        <p><input type="button" id="selectReqs" alt="/Content/tmpl/RequirementIndex.html" value="@T("选择子需求")" /></p>
    }
        <p><input type="button" id="createReqs" alt="/Content/tmpl/RequirementEdit.html" value="@T("创建子需求")" /></p>
        
    @if (ViewData.ContainsKey("ProjectId"))
    {
        <p><input type="button" id="selectTask" alt="/Content/tmpl/TaskIndex.html" value="@T("选择子任务")" /></p>
    }
        <p><input type="button" id="createTask" alt="/Content/tmpl/TaskEdit.html" value="@T("创建子任务")" /></p>
</div>

@{
    this.WriteClientTemplate("Requirement", "Edit");
    this.WriteClientTemplateScript();
}

<script type="text/html" id="requirement_tmpl">	
    <li class="reference">
        <strong>名称：</strong>(<a href="/Requirement/Details/${ID}">${ID}</a>): ${Title} - 
        <a href="javascript:" class="delete_btn">删除</a>
        <input type="hidden" value="${ID}" id="Children" name="Children" />
    </li>
</script>
<script type="text/html" id="task_tmpl">	
    <li class="reference">
        <strong>名称：</strong>(<a href="/Task/Details/${ID}">${ID}</a>): ${Title} - 
        <a href="javascript:" class="delete_btn">删除</a>
        <input type="hidden" value="${ID}" id="Children" name="Children" />
    </li>
</script>
<script type="text/html" id="milestone_tmpl">	
    <select name="BelongsToMilestone.ID" id="BelongsToMilestone.ID">
        {{each dataContext}}
        <option value="${ID}">${Title}</option>
        {{/each}}
    </select>
</script>
<script type="text/html" id="ticket_status_tmpl">	
    <select name="Status" id="Status">
        {{each dataContext}}
        <option value="$value">$value</option>
        {{/each}}
    </select>
</script>

<div class="subform">
    <form id="child_editor">
    </form>
</div>
<script language="javascript" type="text/javascript">
    @{        
        var milestoneText = new HtmlString((string)ViewData["Milestones"]);   
    }
    var tmpl_req_obj = { 
        Title: null,
        StartDate: null,
        DueDate: null,
        Children: [{ID: ""}],
        BelongsToMilestone: {ID: ""}  
    };
    var tmpl_task_obj = {
        Title: null,
        StartDate: null,
        DueDate: null,
        BelongsToMilestone: {ID: ""}  
    };

    register_submit_handler(tmpl_req_obj, function(){
        window.location = "/Requirement/Details/@Model.ID";
    });
    
    function registerHandler(selector, callback) {
        $(selector).click(function () {
            var url = $(this).attr('alt');
            openModelWindow(url, callback);
        });
    };

    registerHandler('#createReqs', function(data) {  
         creatingChild('/Requirement/Create', data, '#requirement_tmpl', tmpl_req_obj); 

         var editor = $('#child_editor');
         container = $(editor).find('#milestone_list');
         $('#milestone_tmpl').tmpl({ dataContext: milestones }).appendTo(container);
         $('#child_editor').find('#submit_btn').remove();
    });

    registerHandler('#createTask', function(data) {
         creatingChild('/Task/Create', data, '#task_tmpl', tmpl_task_obj);
         var editor = $('#child_editor');
         container = $(editor).find('#milestone_list');
         $('#milestone_tmpl').tmpl({ dataContext: milestones }).appendTo(container);
         $('#child_editor').find('#submit_btn').remove();
    });

    $('#addChildren').click(function() {
         $('#action_dialog').dialog();
    });

    // 添加milestone 列表的支持
    var milestones = @milestoneText;
    $('#milestone_tmpl').tmpl({ dataContext: milestones }).appendTo('#milestone_list');

    @if (@Model.BelongsToMilestone != null)
    {
        Html.Raw(string.Format("$.each($('BelongsToMilestone.ID').children('option'), function(i, o) {{ " +
             "    if ( $(this).val() == '{0}' ) {{ " +
             "         $(this).attr('selected', 'selected'); " +
             "         return; " +
             "    }} " +
             "}});", Model.BelongsToMilestone.ID));
    }    

    $.each($('#Requirement_Status').children('option'), function(i, o) {
       if ( $(this).val() == '@Model.Status' ) {
           $(this).attr('selected', 'selected');
           return;
       }
    });

    function remove_prev_input(event) {
        var item = event.target;
        $(item).parent("li").remove();
    };

    // 给子任务、子需求等条件设置删除按钮的方法
    $(".delete_btn").click(remove_prev_input);
</script>
    
@if (ViewData.ContainsKey("ProjectId"))
{
    var pid = ViewData["ProjectId"];
    
<script language="javascript" type="text/javascript">
    registerHandler('#selectReqs', function (data) {
        selectingChildItem('/Requirement/UnAssigned/?pid=@pid', data, '#requirement_tmpl');
    });
    registerHandler('#selectTask', function(data) {
        selectingChildItem('/Task/UnAssigned/?pid=@pid', data, '#task_tmpl');
    });
</script>
}